package com.lagou;

import com.lagou.bean.ComsumerComponet;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class AnnotationConsumerMain  {
    public static void main(String[] args) throws  Exception {
        System.out.println("-------------");
        AnnotationConfigApplicationContext   context = new AnnotationConfigApplicationContext(ConsumerConfiguration.class);
        context.start();
        // 获取消费者组件
        ComsumerComponet  service = context.getBean(ComsumerComponet.class);
        //多线程调用三个方法
        ThreadPoolExecutor threadPool = new ThreadPoolExecutor(200, 300, 1, TimeUnit.SECONDS, new LinkedBlockingDeque<>());

        for (int i=0; i < 6000;i++){
            Thread.sleep(20);
            threadPool.execute(()->{
                try {
                    String methodA = service.sayHai("methodA");
                    String methodB = service.sayNiHao("methodB");
                    String methodC = service.sayNiHaoA("methodC");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });
        }


        // 判断线程是否执行完毕
        threadPool.shutdown();
        try{
            while (true) {
                if (threadPool.isTerminated()) {
                    System.out.println(">>>>>>>>>>>>>>>>>>>>>>> 多线程测试性能中 >>>>>>>>>>>>>>>>>>>>>>>");
                    break;
                }
                Thread.sleep(200);
            }
        }catch (Exception e){
            System.out.println("多线程测试性能出错");
            e.printStackTrace();
        }
       /* while(true){
             System.in.read();
             String  hello = service.sayHello("world",1);
             System.out.println("result:"+hello);
        }*/
    }
    @Configuration
    @PropertySource("classpath:/dubbo-consumer.properties")
    @ComponentScan(basePackages = "com.lagou.bean")
    @EnableDubbo
    static  class  ConsumerConfiguration{

    }
}
